package com.shu.consumer_producer;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author ssm
 * @version V1.0
 * @description:
 * @date 2020/8/25 17:35
 */

public class ConditionDemo {

    public static void main(String[] args) {

        ReentrantLock reentrantLock = new ReentrantLock();
        Condition condition = reentrantLock.newCondition();


        new Thread(() -> {
            reentrantLock.lock();
            System.out.println(Thread.currentThread().getName() + "get lock successfully");
            System.out.println(Thread.currentThread().getName() + "waiting signal");
            try {
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " get signal successfully");
            reentrantLock.unlock();
        }, "first thread").start();

        new Thread(() -> {

            reentrantLock.lock();
            System.out.println(Thread.currentThread().getName()+"get lock successfully");

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(Thread.currentThread().getName()+"send signal");
            condition.signalAll();
            System.out.println(Thread.currentThread().getName()+" get signal successfully");
            reentrantLock.unlock();
        }, "second thread").start();


    }
}
